﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public partial class LoanComment : System.Web.UI.UserControl
{
    string appPath = string.Empty;
 
    int parameterX = 0;
    public int ParameterX
    {
        set { parameterX = value; }
    }

    int parameterY = 0;
    public int ParameterY
    {
        set { parameterY = value; }
    }

    public Credits.DataObjects.LoanObject CurrentCredit
    {
        get { return Session[this.ClientID + "CurrentCredit"] != null ? (Credits.DataObjects.LoanObject)Session[this.ClientID + "CurrentCredit"] : new Credits.DataObjects.LoanObject(); }
        set { Session[this.ClientID + "CurrentCredit"] = value; }
    }

    public List<int> ComitetMembers
    {
        get { return ViewState[this.ClientID + "ComitetMembers"] != null ? (List<int>)ViewState[this.ClientID + "ComitetMembers"] : new List<int>(); }
        set { ViewState[this.ClientID + "ComitetMembers"] = value; }
    }

    public void FIllAllComments()
    {
        int loanID = this.CurrentCredit.LoanID;

        if (loanID != 0)
        {
            DataTable messages = Utils.ModuleCredits().GetAprovesListStatusByLoanID(loanID);
            bodyMessageDiv.InnerHtml = GetHTMLTextWithMessage(messages);
        }
    }

    public void Show()
    {
        //userIDHiddenField.Value = string.Empty;
        //messageIDHiddenField.Value = string.Empty;
        commentTextBox.Text = string.Empty;
        bodyMessageDiv.InnerHtml = string.Empty;
        FIllAllComments();

        bool allowEdit = this.ComitetMembers.Contains(Utils.UserObject().UserID);
        saveButton.Enabled = allowEdit;
        messageTypeStatusDDL.Enabled = allowEdit;

        this.commentModalPopup.Show();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        titleLabel.Text = "Lista comentariilor inregistrate ";
        if (!IsPostBack)
        {
            bodyMessageDiv.Style.Add("overflow-y", "scroll");
            bodyMessageDiv.Style.Add("padding", "5px");
            FillMessageTypeStatusDDL();
        }
        //else
        //{
        //    string functionName = Request.Params.Get("__EVENTTARGET");

        //    if (!functionName.Equals(string.Empty))
        //    {
        //        string inputParams = Request.Params.Get("__EVENTARGUMENT");
        //        switch (functionName)
        //        {
        //            case "EditMessage":
        //                this.commentModalPopup.Show();
        //                string[] splitedKeys = inputParams.Split('&');
        //                if (splitedKeys.Length == 3)
        //                {
        //                    string strUserID = splitedKeys[0];
        //                    string strMessageID = splitedKeys[1];
        //                    string strAproveStatusID = splitedKeys[2];

        //                    userIDHiddenField.Value = strUserID;
        //                    messageIDHiddenField.Value = strMessageID;

        //                    int aproveStatusID = 0;
        //                    int.TryParse(strAproveStatusID, out aproveStatusID);

        //                    int messageID = 0;
        //                    int.TryParse(strMessageID, out messageID);

        //                    try
        //                    {
        //                        messageTypeStatusDDL.Enabled = false;
        //                        messageTypeStatusDDL.SelectedValue = strAproveStatusID;                                
        //                    }
        //                    catch { }

        //                    commentTextBox.Text = Utils.ModuleCredits().GetCommentByParameters(this.CurrentCredit.LoanID, Utils.UserObject().UserID, messageID);                                                       
        //                }                        
        //                break;

        //            default:
        //                break;
        //        }
        //    }
        //}
    }

    protected void FillMessageTypeStatusDDL()
    {
        DataTable sourceTable = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.LoanAproveStatus);
        Utils.FillSelector(messageTypeStatusDDL, sourceTable, "Name", "Code");
    }

    protected void saveButton_Click(object sender, EventArgs e)
    {
        this.commentModalPopup.Show();
        //bool isNewComment = userIDHiddenField.Value.Equals(string.Empty) && messageIDHiddenField.Value.Equals(string.Empty);

        DateTime date = DateTime.Now;
        int aproveStatusID = 0;
        int.TryParse(messageTypeStatusDDL.SelectedValue, out aproveStatusID);

        //int messageID = 0;
        //int.TryParse(messageIDHiddenField.Value, out messageID);

        string comment = commentTextBox.Text;

        //bool resultOperation = false;

        //if(isNewComment)
        //    resultOperation = Utils.ModuleCredits().AddCommentByParameters(this.CurrentCredit.LoanID, Utils.UserObject().UserID, date, aproveStatusID, comment);
        ////else
        //    resultOperation = Utils.ModuleCredits().UpdateCommentByParameters(this.CurrentCredit.LoanID, Utils.UserObject().UserID, messageID, comment);

        if (Utils.ModuleCredits().AddCommentByParameters(this.CurrentCredit.LoanID, Utils.UserObject().UserID, date, aproveStatusID, comment))
        {
            Utils.ModuleCredits().TestForAllAproves(this.CurrentCredit);

            FIllAllComments(); 
            commentTextBox.Text = string.Empty;
            //userIDHiddenField.Value = string.Empty;
            //messageIDHiddenField.Value = string.Empty;
            messageTypeStatusDDL.Enabled = true;

            if (MessagesUpdated != null)
            {
                LoanCommentEventsArg args = new LoanCommentEventsArg(true);
                MessagesUpdated(this, args);
            }
        }
        else
        { titleLabel.Text = Utils.ModuleCredits().LastError; }     
    }

    //protected void newCommentButton_Click(object sender, EventArgs e)
    //{
    //    userIDHiddenField.Value = string.Empty;
    //    messageIDHiddenField.Value = string.Empty;
    //    messageTypeStatusDDL.Enabled = true;
    //    commentTextBox.Text = string.Empty;
    //    titleLabel.Text = "Lista comentariilor inregistrate ";
    //    this.commentModalPopup.Show();
    //}

    protected string GetHTMLTextWithMessage(DataTable sourceMessagesTable)
    {
        string result = string.Empty;
        int curentUserID = Utils.UserObject().UserID;

        if (sourceMessagesTable != null && sourceMessagesTable.Rows.Count > 0)
        {
            for (int i = 0; i < sourceMessagesTable.Rows.Count; i++)
            {
                string userAnddateModification = sourceMessagesTable.Rows[i]["UserDate"].ToString();
                string Comment = sourceMessagesTable.Rows[i]["Comment"].ToString();
                int userID = (int)sourceMessagesTable.Rows[i]["UserID"];
                string ApruveStatusID_string = sourceMessagesTable.Rows[i]["ApruveStatusID_string"].ToString();
                string RowKey = sourceMessagesTable.Rows[i]["RowKey"].ToString();
                int aproveStatusID = (int)sourceMessagesTable.Rows[i]["ApruveStatusID"];

                result += "<div style=\"text-align:left; width:90%; float:left;\">" + userAnddateModification + "</div>";
                result += "<div style=\"text-align: right; width:10%; float: left;\">"
                        //+ ( curentUserID == userID ?
                        //"<label onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\" style=\"cursor: pointer; text-decoration: none;\" "
                        //+ " onclick=\"javascript:__doPostBack('EditMessage','" + RowKey + "')\" >"
                        //+ " EDIT</label>" : "&nbsp;&nbsp;")
                        + "</div>";
                result += "<div style=\"text-align:left; padding-left:20px;\">" + Comment + "</div>";
                result += "<div style=\"text-align: right; width:100%;\">" + ApruveStatusID_string + "</div>";
                result += "<hr>";
                result += "<div style=\"clear:both;\"></div>";
            }
        }

        return result;
    }

    public class LoanCommentEventsArg : EventArgs
    {
        private bool mesageStatusUpdated;
        public bool MesageStatusUpdated
        {
            get { return mesageStatusUpdated; }
        }

        public LoanCommentEventsArg(bool eventPased)
        {
            mesageStatusUpdated = eventPased;
        }
    }

    public event CustomItemEventHandler MessagesUpdated;
    public delegate void CustomItemEventHandler(object sender, LoanComment.LoanCommentEventsArg e);
}
